# encoding=utf-8
from selenium.webdriver.support.ui import WebDriverWait


class GeneralFunction:
    '''
    通用的业务方法，切换窗口,frame等，处理页面内的所有操作
    '''

    def close_newwindowhandle(self, dr, oldwindows, newwindows):
        '''
        通过比对原有窗口和现有窗口，判断新打开的窗口，关闭新打开窗口
        :param dr:driver
        :param oldwindows:原有窗口信息
        :param newwindows: 现有窗口信息
        :return:
        '''
        for windowhand in newwindows:
            if windowhand not in oldwindows:
                dr.switch_to.window(windowhand)
                dr.close()

    def switch_newwindowhandle(self, dr, oldwindows, newwindows):
        '''
        通过比对原有窗口和现有窗口，判断新打开的窗口，切换到新窗口
        :param dr:driver
        :param oldwindows:原有窗口信息
        :param newwindows: 现有窗口信息
        :return:
        '''
        for windowhand in newwindows:
            if windowhand not in oldwindows:
                dr.switch_to.window(windowhand)

    def get_alert_text(self, dr):
        '''
        返回alert框的信息
        :param dr:
        :return:
        '''
        return dr.switch_to.alert.text

    def clearAndSendKeyForText(self, att, text, *args):
        """
        文本框进行清除，然后写入的操作
        :param att: 元素本身
        :param text: 需要输入的文字
        :param args:driver的扩展，用于循环获取焦点，然后清除数据用的
        :return:
        """
        if args.__len__()>0:
            dr = args[0]
            dr.switch_to.active_element
        att.clear()
        att.send_keys(text)
        return text

    def find_element_by_href(self, dr, href):
        '''
        通过模糊匹配href进行返回element
        :param dr:
        :param href:
        :return:
        '''
        return dr.find_element_by_xpath("//*[contains(@href,'{}')]".format(href))
    def find_elements_by_href(self, dr, href):
        '''
        通过模糊匹配href进行返回element
        :param dr:
        :param href:
        :return:
        '''
        return dr.find_elements_by_xpath("//*[contains(@href,'{}')]".format(href))

    def click_element_by_href(self, dr, href):
        '''
        通过模糊匹配href,然后点击
        :param dr:
        :param href:
        :return:
        '''
        dr.find_element_by_xpath("//*[contains(@href,'{}')]".format(href)).click()

    def openNewFrame(self, clicker, frameType, *js):
        """
        :param clicker:被操作的元素
        :param frameType: frame的xpath的type，举例：iframe或frame
        :param js: 可能被执行的js
        :return:
        """
        isin = False
        oldframe = self.dr.find_elements_by_xpath(frameType)
        clicker.click()
        if len(js) > 0:
            self.dr.execute_script(js[0])
        newframe = self.dr.find_elements_by_xpath(frameType)
        for frame in newframe:
            if frame not in oldframe:
                self.dr.switch_to.frame(frame)
                isin = True
        return isin

    def webdriverwait(self, dr, element):
        """
        等待显示出来
        :param dr:驱动
        :param element:展示对象
        :return:
        """
        WebDriverWait(dr, 30).until(lambda dr: element.is_displayed())

    def webdriverwaitnotenable(self, dr, element):
        """
        等待元素不可点击
        :param dr:驱动
        :param element:展示对象
        :return:
        """
        WebDriverWait(dr, 30).until_not(lambda dr: element.is_enabled())

    def clickByjs(self, dr, ele):
        """
        通过js点击元素，主要用于页面挡住的元素
        :param dr: 驱动
        :param ele: 元素
        :return:
        """
        dr.execute_script('arguments[0].click()', ele)
